using System;
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;

using WHC.Pager.Entity;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WHC.Hotel.Entity;
using WHC.Hotel.IDAL;
using WHC.OrderWater.Commons;

namespace WHC.Hotel.DAL
{
	/// <summary>
	/// TeaSuite 的摘要说明。
	/// </summary>
	public class KTVSuite : BaseDAL<KTVSuiteInfo>, IKTVSuite
	{
		#region 对象实例及构造函数

		public static KTVSuite Instance
		{
			get
			{
				return new KTVSuite();
			}
		}
		public KTVSuite() : base("YL_KTVSuite","ID")
		{
		}

		#endregion

		/// <summary>
		/// 将DataReader的属性值转化为实体类的属性值，返回实体类
		/// </summary>
		/// <param name="dr">有效的DataReader对象</param>
		/// <returns>实体类对象</returns>
		protected override KTVSuiteInfo DataReaderToEntity(IDataReader dataReader)
		{
			KTVSuiteInfo teaSuiteInfo = new KTVSuiteInfo();
			SmartDataReader reader = new SmartDataReader(dataReader);
			
			teaSuiteInfo.ID = reader.GetInt32("ID");
			teaSuiteInfo.RoomNo = reader.GetString("RoomNo");
			teaSuiteInfo.RoomType = reader.GetString("RoomType");
            teaSuiteInfo.Status = ConvertStatus(reader.GetString("Status"));
			teaSuiteInfo.Region = reader.GetString("Region");
			teaSuiteInfo.Waiter = reader.GetString("Waiter");
			teaSuiteInfo.Note = reader.GetString("Note");
			
			return teaSuiteInfo;
		}

		/// <summary>
		/// 将实体对象的属性值转化为Hashtable对应的键值
		/// </summary>
		/// <param name="obj">有效的实体对象</param>
		/// <returns>包含键值映射的Hashtable</returns>
        protected override Hashtable GetHashByEntity(KTVSuiteInfo obj)
		{
		    KTVSuiteInfo info = obj as KTVSuiteInfo;
			Hashtable hash = new Hashtable(); 
			
 			hash.Add("RoomNo", info.RoomNo);
 			hash.Add("RoomType", info.RoomType);
 			hash.Add("Status", info.Status.ToString());
 			hash.Add("Region", info.Region);
 			hash.Add("Waiter", info.Waiter);
 			hash.Add("Note", info.Note);
 				
			return hash;
		}

        private KTVSuiteStatus ConvertStatus(string status)
        {
            KTVSuiteStatus roomStatus = KTVSuiteStatus.可供;
            try
            {
                roomStatus = (KTVSuiteStatus)Enum.Parse(typeof(KTVSuiteStatus), status);
            }
            catch
            {
            }
            return roomStatus;
        } 

        public List<string> GetAllRegions()
        {
            return GetFieldList("Region");
        }

        /// <summary>
        /// 获取所有房间的状态统计数据
        /// </summary>
        /// <returns></returns>
        public KTVSuiteStatusInfo GetStatus()
        {
            string sql = string.Format("select Status,count(Status) as Count from {0} group by Status", tableName);
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);

            KTVSuiteStatusInfo info = new KTVSuiteStatusInfo();
            using (IDataReader dr = db.ExecuteReader(command))
            {
                while (dr.Read())
                {
                    #region 解析各种房间状态
                    string status = dr["Status"].ToString();
                    switch (status)
                    {
                        case "可供":
                            info.Availables = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "停用":
                            info.Stops = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "预定":
                            info.BookIns = Convert.ToInt32(dr["Count"].ToString());
                            break;
                        case "占用":
                            info.Occupies = Convert.ToInt32(dr["Count"].ToString());
                            break;
                    }
                    #endregion
                }
            }
            info.All = info.Stops + info.Availables + info.BookIns + info.Occupies;
            if (info.All > 0)
            {
                double rate = Convert.ToDouble(info.Occupies) / info.All;
                info.InRoomRate = rate.ToString("P2");
            }
            return info;
        }

        /// <summary>
        /// 设置房间的状态
        /// </summary>
        /// <param name="roomStatus">房间状态</param>
        /// <returns></returns>
        public bool SetRoomStatus(string roomNo, string roomStatus)
        {
            string sql = string.Format("Update {0} Set Status='{1}' where RoomNo='{2}' ",
                this.tableName, roomStatus, roomNo);

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand command = db.GetSqlStringCommand(sql);
            return db.ExecuteNonQuery(command) > 0;
        }

        /// <summary>
        /// 根据房间号获取对应的房间对象
        /// </summary>
        /// <param name="roomNo"></param>
        /// <returns></returns>
        public KTVSuiteInfo FindByRoomNo(string roomNo)
        {
            string condition = string.Format("RoomNo='{0}' ", roomNo);
            return this.FindSingle(condition);
        }

    }
}